package internal

import (
	utils2 "MoSkeleton/framework/utils"
	"MoSkeleton/global/utils"
	system2 "MoSkeleton/web/pogos/respose/system"
	mapset "github.com/deckarep/golang-set/v2"
	"github.com/gin-gonic/gin"
	"github.com/samber/lo"
)

type OnlineAuthSubject struct {
}

func NewAuthSubject() *OnlineAuthSubject {
	return &OnlineAuthSubject{}
}

func (o *OnlineAuthSubject) GetPermissions(c *gin.Context) mapset.Set[string] {
	userId := utils2.GetUserIdFromGin(c)
	onlineUser := utils.GetSetOnlineUser(c, userId)
	if onlineUser == nil {
		return mapset.NewSet[string]()
	}
	return *onlineUser.Permissions
}
func (o *OnlineAuthSubject) GetRoles(c *gin.Context) mapset.Set[string] {
	userId := utils2.GetUserIdFromGin(c)
	onlineUser := utils.GetSetOnlineUser(c, userId)
	if onlineUser == nil {
		return mapset.NewSet[string]()
	}
	set := mapset.NewSet[string]()
	lo.ForEach(onlineUser.User.Roles, func(item *system2.SysUserDetailRoleItemResp, index int) {
		set.Add(item.RoleKey)
	})
	return set
}
